/*
 * @(#)FormSection.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.form.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.dcivision.form.bean.FormSection;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.framework.dao.AbstractDAObject;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.workflow.bean.WorkflowStep;
import com.dcivision.workflow.dao.MtmWfStepFormRecordDAObject;

/**
  FormSectionDAObject.java

  This class is the data access bean for table "FORM_SECTION".

  @author      Scott Tong
  @company     DCIVision Limited
  @creation date   20/12/2003
  @version     $Revision: 1.27 $
*/

public class FormSectionDAObject extends AbstractDAObject {

  public static final String REVISION = "$Revision: 1.27 $";

  public static final String TABLE_NAME = "FORM_SECTION";

  public FormSectionDAObject(SessionContainer sessionContainer, Connection dbConn) {
    super(sessionContainer, dbConn);
  }

  protected void initDBSetting() {
    this.baseTableName = TABLE_NAME;
    this.vecDBColumn.add("ID");
    this.vecDBColumn.add("TITLE");
    this.vecDBColumn.add("TITLE_ALT");
    this.vecDBColumn.add("DESCRIPTION");
    this.vecDBColumn.add("DESCRIPTION_ALT");
    this.vecDBColumn.add("RECORD_STATUS");
    this.vecDBColumn.add("SUB_SECTION");
    this.vecDBColumn.add("UPDATE_COUNT");
    this.vecDBColumn.add("CREATOR_ID");
    this.vecDBColumn.add("CREATE_DATE");
    this.vecDBColumn.add("UPDATER_ID");
    this.vecDBColumn.add("UPDATE_DATE");
  }

  protected synchronized AbstractBaseObject getByID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.TITLE, A.TITLE_ALT, A.DESCRIPTION, A.DESCRIPTION_ALT, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE , A.SECTION_FONT_COLOR,A.SECTION_FONT_SIZE, A.SECTION_FONT_TYPE,A.SECTION_FONT_STYLE,A.SECTION_POSITION,A.SUB_SECTION ");
        sqlStat.append("FROM   FORM_SECTION A ");
        sqlStat.append("WHERE  A.ID = ? AND A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        if (rs.next()) {
          FormSection tmpFormSection = new FormSection();
          tmpFormSection.setID(getResultSetInteger(rs, "ID"));
          tmpFormSection.setTitle(getResultSetString(rs, "TITLE"));
          tmpFormSection.setTitleAlt(getResultSetString(rs, "TITLE_ALT"));
          tmpFormSection.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpFormSection.setDescriptionAlt(getResultSetString(rs, "DESCRIPTION_ALT"));
          tmpFormSection.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpFormSection.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpFormSection.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpFormSection.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpFormSection.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpFormSection.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpFormSection.setsectionFontColor(getResultSetString(rs, "SECTION_FONT_COLOR"));
          tmpFormSection.setsectionFontSize(getResultSetString(rs, "SECTION_FONT_SIZE"));
          tmpFormSection.setsectionFontType(getResultSetString(rs, "SECTION_FONT_TYPE"));
          tmpFormSection.setsectionFontStyle(getResultSetString(rs, "SECTION_FONT_STYLE"));
          tmpFormSection.setsectionPosition(getResultSetString(rs, "SECTION_POSITION"));
          tmpFormSection.setSubSection(getResultSetString(rs, "SUB_SECTION"));
          tmpFormSection.setCreatorName(UserInfoFactory.getUserFullName(tmpFormSection.getCreatorID()));
          tmpFormSection.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormSection.getUpdaterID()));
          return(tmpFormSection);
        } else {
          throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized List getList(AbstractSearchForm searchForm) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.TITLE, A.TITLE_ALT, A.DESCRIPTION, A.DESCRIPTION_ALT, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE, A.SECTION_FONT_COLOR,A.SECTION_FONT_SIZE, A.SECTION_FONT_TYPE,A.SECTION_FONT_STYLE,A.SECTION_POSITION,A.SUB_SECTION  ");
        sqlStat.append("FROM   FORM_SECTION A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");

        // Form the WHERE clause for filtering.
        if (searchForm.isSearchable()) {
          String searchField = getSearchColumn(searchForm.getBasicSearchField());
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }

        //format the sql for any 'LIKE' statement contained
        sqlStat = this.getFormattedSQL(sqlStat.toString());

        // Form the ORDER clause for sorting.
        if (searchForm.isSortable()) {
          String sortAttribute = searchForm.getSortAttribute();
          if (sortAttribute.indexOf(".") < 0) {
            sortAttribute = "A." + sortAttribute;
          }
          sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
        }

        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStatCnt, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStatCnt, 2, searchKeyword);
        }
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }

        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStat, 2, searchKeyword);
        }
        rs = preStat.executeQuery();

        this.positionCursor(rs, startOffset, pageSize);

        while (rs.next() && rowLoopCnt<pageSize) {
          FormSection tmpFormSection = new FormSection();
          tmpFormSection.setID(getResultSetInteger(rs, "ID"));
          tmpFormSection.setTitle(getResultSetString(rs, "TITLE"));
          tmpFormSection.setTitleAlt(getResultSetString(rs, "TITLE_ALT"));
          tmpFormSection.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpFormSection.setDescriptionAlt(getResultSetString(rs, "DESCRIPTION_ALT"));
          tmpFormSection.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpFormSection.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpFormSection.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpFormSection.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpFormSection.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpFormSection.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpFormSection.setsectionFontColor(getResultSetString(rs, "SECTION_FONT_COLOR"));
          tmpFormSection.setsectionFontSize(getResultSetString(rs, "SECTION_FONT_SIZE"));
          tmpFormSection.setsectionFontType(getResultSetString(rs, "SECTION_FONT_TYPE"));
          tmpFormSection.setsectionFontStyle(getResultSetString(rs, "SECTION_FONT_STYLE"));
          tmpFormSection.setsectionPosition(getResultSetString(rs, "SECTION_POSITION"));
          tmpFormSection.setSubSection(getResultSetString(rs, "SUB_SECTION")) ;
          tmpFormSection.setCreatorName(UserInfoFactory.getUserFullName(tmpFormSection.getCreatorID()));
          tmpFormSection.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormSection.getUpdaterID()));
          tmpFormSection.setRecordCount(totalNumOfRecord);
          tmpFormSection.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpFormSection);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
      }
    }
  }

  protected synchronized List getList() throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.TITLE, A.TITLE_ALT, A.DESCRIPTION, A.DESCRIPTION_ALT, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE, A.SECTION_FONT_COLOR,A.SECTION_FONT_SIZE, A.SECTION_FONT_TYPE,A.SECTION_FONT_STYLE,A.SECTION_POSITION,A.SUB_SECTION   ");
        sqlStat.append("FROM   FORM_SECTION A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          FormSection tmpFormSection = new FormSection();
          tmpFormSection.setID(getResultSetInteger(rs, "ID"));
          tmpFormSection.setTitle(getResultSetString(rs, "TITLE"));
          tmpFormSection.setTitleAlt(getResultSetString(rs, "TITLE_ALT"));
          tmpFormSection.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpFormSection.setDescriptionAlt(getResultSetString(rs, "DESCRIPTION_ALT"));
          tmpFormSection.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpFormSection.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpFormSection.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpFormSection.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpFormSection.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpFormSection.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpFormSection.setsectionFontColor(getResultSetString(rs, "SECTION_FONT_COLOR"));
          tmpFormSection.setsectionFontSize(getResultSetString(rs, "SECTION_FONT_SIZE"));
          tmpFormSection.setsectionFontType(getResultSetString(rs, "SECTION_FONT_TYPE"));
          tmpFormSection.setsectionFontStyle(getResultSetString(rs, "SECTION_FONT_STYLE"));
          tmpFormSection.setsectionPosition(getResultSetString(rs, "SECTION_POSITION"));
          tmpFormSection.setSubSection(getResultSetString(rs, "SUB_SECTION"));

          tmpFormSection.setCreatorName(UserInfoFactory.getUserFullName(tmpFormSection.getCreatorID()));
          tmpFormSection.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormSection.getUpdaterID()));
          result.add(tmpFormSection);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected void validateInsert(AbstractBaseObject obj) throws ApplicationException {
  }

  protected void validateUpdate(AbstractBaseObject obj) throws ApplicationException {
  }

  protected void validateDelete(AbstractBaseObject obj) throws ApplicationException {
  }

  protected synchronized AbstractBaseObject insert(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    FormSection tmpFormSection = (FormSection)((FormSection)obj).clone();

    synchronized(dbConn) {
      try {
        Integer nextID = getNextPrimaryID();
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("INSERT ");
        sqlStat.append("INTO   FORM_SECTION(ID, TITLE, TITLE_ALT, DESCRIPTION, DESCRIPTION_ALT, RECORD_STATUS, UPDATE_COUNT, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE,SUB_SECTION,SECTION_FONT_COLOR,SECTION_FONT_SIZE,SECTION_FONT_TYPE,SECTION_POSITION,SECTION_FONT_STYLE ) ");
        sqlStat.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ,?,?,?,?,? ) ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, nextID);
        setPrepareStatement(preStat, 2, tmpFormSection.getTitle());
        setPrepareStatement(preStat, 3, tmpFormSection.getTitleAlt());
        setPrepareStatement(preStat, 4, tmpFormSection.getDescription());
        setPrepareStatement(preStat, 5, tmpFormSection.getDescriptionAlt());
        setPrepareStatement(preStat, 6, GlobalConstant.RECORD_STATUS_ACTIVE);
        setPrepareStatement(preStat, 7, new Integer(0));
        setPrepareStatement(preStat, 8, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 9, currTime);
        setPrepareStatement(preStat, 10, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 11, currTime);
        setPrepareStatement(preStat, 12, tmpFormSection.getSubSection());
        setPrepareStatement(preStat, 13, tmpFormSection.getsectionFontColor());
        setPrepareStatement(preStat, 14, TextUtility.parseIntegerObj(tmpFormSection.getsectionFontSize()));
        setPrepareStatement(preStat, 15, tmpFormSection.getsectionFontType());
        setPrepareStatement(preStat, 16, tmpFormSection.getsectionPosition());
        setPrepareStatement(preStat, 17, tmpFormSection.getsectionFontStyle());
        preStat.executeUpdate();
        tmpFormSection.setID(nextID);
        tmpFormSection.setCreatorID(sessionContainer.getUserRecordID());
        tmpFormSection.setCreateDate(currTime);
        tmpFormSection.setUpdaterID(sessionContainer.getUserRecordID());
        tmpFormSection.setUpdateDate(currTime);
        tmpFormSection.setUpdateCount(new Integer(0));
        tmpFormSection.setCreatorName(UserInfoFactory.getUserFullName(tmpFormSection.getCreatorID()));
        tmpFormSection.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormSection.getUpdaterID()));
        return(tmpFormSection);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_INSERT_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }


  protected synchronized AbstractBaseObject update(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    FormSection tmpFormSection = (FormSection)((FormSection)obj).clone();
    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("UPDATE FORM_SECTION ");
        sqlStat.append("SET  TITLE=?, TITLE_ALT=?, DESCRIPTION=?, DESCRIPTION_ALT=?, UPDATE_COUNT=?, UPDATER_ID=?, UPDATE_DATE=?,SECTION_FONT_COLOR=? "+
                       " ,SECTION_FONT_SIZE=?, SECTION_FONT_TYPE=?,SECTION_FONT_STYLE=?,SECTION_POSITION=?,SUB_SECTION=?  ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpFormSection.getTitle());
        setPrepareStatement(preStat, 2, tmpFormSection.getTitleAlt());
        setPrepareStatement(preStat, 3, tmpFormSection.getDescription());
        setPrepareStatement(preStat, 4, tmpFormSection.getDescriptionAlt());
        setPrepareStatement(preStat, 5, new Integer(tmpFormSection.getUpdateCount().intValue() + 1));
        setPrepareStatement(preStat, 6, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 7, currTime);
        setPrepareStatement(preStat, 8, tmpFormSection.getsectionFontColor());
        setPrepareStatement(preStat, 9, TextUtility.parseIntegerObj(tmpFormSection.getsectionFontSize()));
        setPrepareStatement(preStat, 10, tmpFormSection.getsectionFontType());
        setPrepareStatement(preStat, 11, tmpFormSection.getsectionFontStyle());
        setPrepareStatement(preStat, 12, tmpFormSection.getsectionPosition());
        setPrepareStatement(preStat, 13, tmpFormSection.getSubSection());
        setPrepareStatement(preStat, 14, tmpFormSection.getID());
        setPrepareStatement(preStat, 15, tmpFormSection.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          tmpFormSection.setUpdaterID(sessionContainer.getUserRecordID());
          tmpFormSection.setUpdateDate(currTime);
          tmpFormSection.setUpdateCount(new Integer(tmpFormSection.getUpdateCount().intValue() + 1));
          tmpFormSection.setCreatorName(UserInfoFactory.getUserFullName(tmpFormSection.getCreatorID()));
          tmpFormSection.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormSection.getUpdaterID()));
          return(tmpFormSection);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }


  public synchronized void updateByFile(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    FormSection tmpFormSection = (FormSection)((FormSection)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("UPDATE FORM_SECTION ");
        sqlStat.append("SET  TITLE='"+tmpFormSection.getTitle()+"', ");
       if (tmpFormSection.getsectionFontColor()!=null)
        sqlStat.append(" SECTION_FONT_COLOR='"+tmpFormSection.getsectionFontColor()+"',   ");
       if (tmpFormSection.getsectionFontSize()!=null)
        sqlStat.append(" SECTION_FONT_SIZE="+tmpFormSection.getsectionFontSize()+",   ");
       if (tmpFormSection.getsectionFontType()!=null)
        sqlStat.append(" SECTION_FONT_TYPE='"+tmpFormSection.getsectionFontType()+"' ,   ");
       if (tmpFormSection.getsectionFontStyle()!=null)
        sqlStat.append(" SECTION_FONT_STYLE='"+tmpFormSection.getsectionFontStyle()+"',   ");
       if (tmpFormSection.getsectionPosition()!=null)
        sqlStat.append(" SECTION_POSITION='"+tmpFormSection.getsectionPosition()+"',   ");
       if (tmpFormSection.getSubSection()!=null)
          sqlStat.append(" SUB_SECTION='"+tmpFormSection.getSubSection()+"',   ");
          sqlStat.append(" UPDATE_COUNT=UPDATE_COUNT+1  WHERE  ID="+tmpFormSection.getID());

        preStat = dbConn.prepareStatement(sqlStat.toString());
        preStat.executeUpdate();
        dbConn.commit();
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized AbstractBaseObject delete(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    FormSection tmpFormSection = (FormSection)((FormSection)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   FORM_SECTION ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpFormSection.getID());
        setPrepareStatement(preStat, 2, tmpFormSection.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          return(tmpFormSection);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized void auditTrail(String opMode, AbstractBaseObject obj) throws ApplicationException {
    Vector oldValues = new Vector();
    Vector newValues = new Vector();
    FormSection tmpFormSection = (FormSection)this.oldValue;

    if (tmpFormSection != null) {
      oldValues.add(toAuditTrailValue(tmpFormSection.getID()));
      oldValues.add(toAuditTrailValue(tmpFormSection.getTitle()));
      oldValues.add(toAuditTrailValue(tmpFormSection.getTitleAlt()));
      oldValues.add(toAuditTrailValue(tmpFormSection.getDescription()));
      oldValues.add(toAuditTrailValue(tmpFormSection.getDescriptionAlt()));
      oldValues.add(toAuditTrailValue(tmpFormSection.getSubSection()));
      oldValues.add(toAuditTrailValue(tmpFormSection.getRecordStatus()));
      oldValues.add(toAuditTrailValue(tmpFormSection.getUpdateCount()));
      oldValues.add(toAuditTrailValue(tmpFormSection.getCreatorID()));
      oldValues.add(toAuditTrailValue(tmpFormSection.getCreateDate()));
      oldValues.add(toAuditTrailValue(tmpFormSection.getUpdaterID()));
      oldValues.add(toAuditTrailValue(tmpFormSection.getUpdateDate()));
    }

    tmpFormSection = (FormSection)obj;
    if (tmpFormSection != null) {
      newValues.add(toAuditTrailValue(tmpFormSection.getID()));
      newValues.add(toAuditTrailValue(tmpFormSection.getTitle()));
      newValues.add(toAuditTrailValue(tmpFormSection.getTitleAlt()));
      newValues.add(toAuditTrailValue(tmpFormSection.getDescription()));
      newValues.add(toAuditTrailValue(tmpFormSection.getDescriptionAlt()));
      newValues.add(toAuditTrailValue(tmpFormSection.getSubSection()));
      newValues.add(toAuditTrailValue(tmpFormSection.getRecordStatus()));
      newValues.add(toAuditTrailValue(tmpFormSection.getUpdateCount()));
      newValues.add(toAuditTrailValue(tmpFormSection.getCreatorID()));
      newValues.add(toAuditTrailValue(tmpFormSection.getCreateDate()));
      newValues.add(toAuditTrailValue(tmpFormSection.getUpdaterID()));
      newValues.add(toAuditTrailValue(tmpFormSection.getUpdateDate()));
    }

    auditTrailBase(opMode, oldValues, newValues);
  }

  /***********************************************************************
   * DON'T Modify the codes above unless you know what you are doing!!!  *
   * Put your own functions beblow.                                      *
   * For FINDER methods, the function name should be in the notation:    *
   *   public Object getObjectBy<Criteria>()                             *
   *   - e.g. public Object getObjectByCode()                            *
   *   public List getListBy<Criteria>()                                 *
   *   - e.g. public List getListByUserID()                              *
   * For OPERATION methods, the function name should be in the notation: *
   *   public void <Operation>ObjectBy<Criteria>()                       *
   *   - e.g. public void deleteObjectByCode()                           *
   *   public void <Operation>ListBy<Criteria>()                         *
   *   - e.g. public void deleteListByUserID()                           *
   ***********************************************************************/

  public synchronized List getListByFormPageID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.TITLE, A.TITLE_ALT, A.DESCRIPTION, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE,A.SECTION_FONT_COLOR, A.SECTION_FONT_SIZE, A.SECTION_FONT_TYPE, A.SECTION_FONT_STYLE, A.SECTION_POSITION ,A.SUB_SECTION ");
        sqlStat.append("FROM   FORM_SECTION A, MTM_FORM_PAGE_FORM_SECTION B ");
        sqlStat.append("WHERE  A.ID = B.SECTION_ID AND B.PAGE_ID = ? AND A.RECORD_STATUS = ? ");
        sqlStat.append("ORDER BY B.SECTION_SEQ ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          FormSection tmpFormSection = new FormSection();
          tmpFormSection.setID(getResultSetInteger(rs, "ID"));
          tmpFormSection.setTitle(getResultSetString(rs, "TITLE"));
          tmpFormSection.setTitleAlt(getResultSetString(rs, "TITLE_ALT"));
          tmpFormSection.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpFormSection.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpFormSection.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpFormSection.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpFormSection.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpFormSection.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpFormSection.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpFormSection.setsectionFontColor(getResultSetString(rs, "SECTION_FONT_COLOR"));
          tmpFormSection.setsectionFontSize(getResultSetString(rs, "SECTION_FONT_SIZE"));
          tmpFormSection.setsectionFontType(getResultSetString(rs, "SECTION_FONT_TYPE"));
          tmpFormSection.setsectionFontStyle(getResultSetString(rs, "SECTION_FONT_STYLE"));
          tmpFormSection.setsectionPosition(getResultSetString(rs, "SECTION_POSITION"));
          tmpFormSection.setSubSection(getResultSetString(rs, "SUB_SECTION"));

          tmpFormSection.setCreatorName(UserInfoFactory.getUserFullName(tmpFormSection.getCreatorID()));
          tmpFormSection.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormSection.getUpdaterID()));
          result.add(tmpFormSection);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  public synchronized List getDetailListByFormPageID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.TITLE, A.TITLE_ALT, A.DESCRIPTION, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE,  A.SECTION_FONT_COLOR,A.SECTION_FONT_SIZE, A.SECTION_FONT_TYPE,A.SECTION_FONT_STYLE,A.SECTION_POSITION,A.SUB_SECTION  ");
        sqlStat.append("FROM   FORM_SECTION A, MTM_FORM_PAGE_FORM_SECTION B ");
        sqlStat.append("WHERE  A.ID = B.SECTION_ID AND B.PAGE_ID = ? AND A.RECORD_STATUS = ? ");
        sqlStat.append("ORDER BY B.SECTION_SEQ ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          FormSection tmpFormSection = new FormSection();
          tmpFormSection.setID(getResultSetInteger(rs, "ID"));
          tmpFormSection.setTitle(getResultSetString(rs, "TITLE"));
          tmpFormSection.setTitleAlt(getResultSetString(rs, "TITLE_ALT"));
          tmpFormSection.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpFormSection.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpFormSection.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpFormSection.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpFormSection.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpFormSection.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpFormSection.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpFormSection.setsectionFontColor(getResultSetString(rs, "SECTION_FONT_COLOR"));
          tmpFormSection.setsectionFontSize(getResultSetString(rs, "SECTION_FONT_SIZE"));
          tmpFormSection.setsectionFontType(getResultSetString(rs, "SECTION_FONT_TYPE"));
          tmpFormSection.setsectionFontStyle(getResultSetString(rs, "SECTION_FONT_STYLE"));
          tmpFormSection.setsectionPosition(getResultSetString(rs, "SECTION_POSITION"));
          tmpFormSection.setSubSection(getResultSetString(rs, "SUB_SECTION"));

          tmpFormSection.setCreatorName(UserInfoFactory.getUserFullName(tmpFormSection.getCreatorID()));
          tmpFormSection.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormSection.getUpdaterID()));
          result.add(tmpFormSection);
        }

        FormElementDAObject formElementDAO = new FormElementDAObject(sessionContainer, dbConn);
        for (int i=0; i<result.size(); i++) {
          FormSection tmpFormSection = (FormSection) result.get(i);
          List formElementList = formElementDAO.getDetailListByFormSectionID(tmpFormSection.getID());
          tmpFormSection.setFormElementList(formElementList);
          result.set(i, tmpFormSection);
        }

        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  public synchronized List getDetailListByFormRecordIDPageIDStepID(Integer formRecordID, Integer formPageID, Integer workflowStepID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    String permission = "";
    
    synchronized(dbConn) {
      try {
        MtmWfStepFormRecordDAObject mtmWfStepFormRecordDAO = new MtmWfStepFormRecordDAObject(sessionContainer, dbConn);
        FormElementDAObject formElementDAO = new FormElementDAObject(sessionContainer, dbConn);
        
        sqlStat.append("SELECT A.ID, A.TITLE, A.TITLE_ALT, A.DESCRIPTION, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE,  A.SECTION_FONT_COLOR,A.SECTION_FONT_SIZE, A.SECTION_FONT_TYPE,A.SECTION_FONT_STYLE,A.SECTION_POSITION,A.SUB_SECTION ");
        sqlStat.append("FROM   FORM_SECTION A, MTM_FORM_PAGE_FORM_SECTION B ");
        sqlStat.append("WHERE  A.ID = B.SECTION_ID AND B.PAGE_ID = ? AND A.RECORD_STATUS = ? ");
        sqlStat.append("ORDER BY B.SECTION_SEQ ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, formPageID);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          FormSection tmpFormSection = new FormSection();
          tmpFormSection.setID(getResultSetInteger(rs, "ID"));
          tmpFormSection.setTitle(getResultSetString(rs, "TITLE"));
          tmpFormSection.setTitleAlt(getResultSetString(rs, "TITLE_ALT"));
          tmpFormSection.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpFormSection.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpFormSection.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpFormSection.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpFormSection.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpFormSection.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpFormSection.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpFormSection.setsectionFontColor(getResultSetString(rs, "SECTION_FONT_COLOR"));
          tmpFormSection.setsectionFontSize(getResultSetString(rs, "SECTION_FONT_SIZE"));
          tmpFormSection.setsectionFontType(getResultSetString(rs, "SECTION_FONT_TYPE"));
          tmpFormSection.setsectionFontStyle(getResultSetString(rs, "SECTION_FONT_STYLE"));
          tmpFormSection.setsectionPosition(getResultSetString(rs, "SECTION_POSITION"));
          tmpFormSection.setSubSection(getResultSetString(rs, "SUB_SECTION"));
          tmpFormSection.setCreatorName(UserInfoFactory.getUserFullName(tmpFormSection.getCreatorID()));
          tmpFormSection.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormSection.getUpdaterID()));
          if (workflowStepID.intValue()>0){
            permission = mtmWfStepFormRecordDAO.getFormSectionPermissionByWorkflowStepIDFormRecordIDFormPageIDFormSectionID(workflowStepID, formRecordID, formPageID, tmpFormSection.getID());
          }
          tmpFormSection.setPermission(permission);         
          result.add(tmpFormSection);
        }

        
        for (int i=0; i<result.size(); i++) {
          FormSection tmpFormSection = (FormSection) result.get(i);
          List formElementList = formElementDAO.getDetailListByFormRecordIDPageIDSectionIDStepID(formRecordID, formPageID, tmpFormSection.getID(), workflowStepID);
          tmpFormSection.setFormElementList(formElementList);
          result.set(i, tmpFormSection);
        }

        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  /**
   * Get All Section By FormID
   * @param FormID
   * @return List
   * @throws ApplicationException
   */

  public synchronized List getAllSectionByFormID(Integer formRecodID,Integer formPageID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT  A.ID, A.TITLE, A.TITLE_ALT, A.DESCRIPTION, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE,  A.SECTION_FONT_COLOR,A.SECTION_FONT_SIZE, A.SECTION_FONT_TYPE,A.SECTION_FONT_STYLE,A.SECTION_POSITION,A.SUB_SECTION");
        sqlStat.append(" FROM  (MTM_FORM_PAGE_FORM_SECTION B  LEFT JOIN FORM_PAGE C ON (B.PAGE_ID = C.ID )) ");
        sqlStat.append("LEFT JOIN  FORM_SECTION A ON A.ID = B.SECTION_ID WHERE  C.FORM_RECORD_ID = ? AND B.PAGE_ID = ? AND  A.RECORD_STATUS = ? ");

        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, formRecodID);
        this.setPrepareStatement(preStat, 2, formPageID);
        this.setPrepareStatement(preStat, 3, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          FormSection tmpFormSection = new FormSection();
          tmpFormSection.setID(getResultSetInteger(rs, "ID"));
          tmpFormSection.setTitle(getResultSetString(rs, "TITLE"));
          tmpFormSection.setTitleAlt(getResultSetString(rs, "TITLE_ALT"));
          tmpFormSection.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpFormSection.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpFormSection.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpFormSection.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpFormSection.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpFormSection.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpFormSection.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpFormSection.setsectionFontColor(getResultSetString(rs, "SECTION_FONT_COLOR"));
          tmpFormSection.setsectionFontSize(getResultSetString(rs, "SECTION_FONT_SIZE"));
          tmpFormSection.setsectionFontType(getResultSetString(rs, "SECTION_FONT_TYPE"));
          tmpFormSection.setsectionFontStyle(getResultSetString(rs, "SECTION_FONT_STYLE"));
          tmpFormSection.setsectionPosition(getResultSetString(rs, "SECTION_POSITION"));
          tmpFormSection.setSubSection(getResultSetString(rs, "SUB_SECTION"));

          tmpFormSection.setCreatorName(UserInfoFactory.getUserFullName(tmpFormSection.getCreatorID()));
          tmpFormSection.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormSection.getUpdaterID()));
          result.add(tmpFormSection);
        }

        FormElementDAObject formElementDAO = new FormElementDAObject(sessionContainer, dbConn);
        for (int i=0; i<result.size(); i++) {
          FormSection tmpFormSection = (FormSection) result.get(i);
          List formElementList = formElementDAO.getDetailListByFormSectionID(tmpFormSection.getID());
          tmpFormSection.setFormElementList(formElementList);
          result.set(i, tmpFormSection);
        }

        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  public synchronized List getAllSectionByFormID(Integer formRecodID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT  A.ID, A.TITLE, A.TITLE_ALT, A.DESCRIPTION, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE,  A.SECTION_FONT_COLOR,A.SECTION_FONT_SIZE, A.SECTION_FONT_TYPE,A.SECTION_FONT_STYLE,A.SECTION_POSITION,A.SUB_SECTION");
        sqlStat.append(" FROM  (MTM_FORM_PAGE_FORM_SECTION B  LEFT JOIN FORM_PAGE C ON (B.PAGE_ID = C.ID )) ");
        sqlStat.append("LEFT JOIN  FORM_SECTION A ON A.ID = B.SECTION_ID WHERE  C.FORM_RECORD_ID = ? AND  A.RECORD_STATUS = ? ");

        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, formRecodID);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          FormSection tmpFormSection = new FormSection();
          tmpFormSection.setID(getResultSetInteger(rs, "ID"));
          tmpFormSection.setTitle(getResultSetString(rs, "TITLE"));
          tmpFormSection.setTitleAlt(getResultSetString(rs, "TITLE_ALT"));
          tmpFormSection.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpFormSection.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpFormSection.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpFormSection.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpFormSection.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpFormSection.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpFormSection.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpFormSection.setsectionFontColor(getResultSetString(rs, "SECTION_FONT_COLOR"));
          tmpFormSection.setsectionFontSize(getResultSetString(rs, "SECTION_FONT_SIZE"));
          tmpFormSection.setsectionFontType(getResultSetString(rs, "SECTION_FONT_TYPE"));
          tmpFormSection.setsectionFontStyle(getResultSetString(rs, "SECTION_FONT_STYLE"));
          tmpFormSection.setsectionPosition(getResultSetString(rs, "SECTION_POSITION"));
          tmpFormSection.setSubSection(getResultSetString(rs, "SUB_SECTION"));

          tmpFormSection.setCreatorName(UserInfoFactory.getUserFullName(tmpFormSection.getCreatorID()));
          tmpFormSection.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormSection.getUpdaterID()));
          result.add(tmpFormSection);
        }

        FormElementDAObject formElementDAO = new FormElementDAObject(sessionContainer, dbConn);
        for (int i=0; i<result.size(); i++) {
          FormSection tmpFormSection = (FormSection) result.get(i);
          List formElementList = formElementDAO.getDetailListByFormSectionID(tmpFormSection.getID());
          tmpFormSection.setFormElementList(formElementList);
          result.set(i, tmpFormSection);
        }

        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  public synchronized List getAllSectionNoSubSecitonByFormID(Integer formRecodID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT  A.ID, A.TITLE, A.TITLE_ALT, A.DESCRIPTION, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE,  A.SECTION_FONT_COLOR,A.SECTION_FONT_SIZE, A.SECTION_FONT_TYPE,A.SECTION_FONT_STYLE,A.SECTION_POSITION,A.SUB_SECTION");
        sqlStat.append(" FROM  (MTM_FORM_PAGE_FORM_SECTION B  LEFT JOIN FORM_PAGE C ON (B.PAGE_ID = C.ID )) ");
        sqlStat.append("LEFT JOIN  FORM_SECTION A ON A.ID = B.SECTION_ID WHERE A.SUB_SECTION = 'N' AND C.FORM_RECORD_ID = ? AND  A.RECORD_STATUS = ? ");

        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, formRecodID);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          FormSection tmpFormSection = new FormSection();
          tmpFormSection.setID(getResultSetInteger(rs, "ID"));
          tmpFormSection.setTitle(getResultSetString(rs, "TITLE"));
          tmpFormSection.setTitleAlt(getResultSetString(rs, "TITLE_ALT"));
          tmpFormSection.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpFormSection.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpFormSection.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpFormSection.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpFormSection.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpFormSection.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpFormSection.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpFormSection.setsectionFontColor(getResultSetString(rs, "SECTION_FONT_COLOR"));
          tmpFormSection.setsectionFontSize(getResultSetString(rs, "SECTION_FONT_SIZE"));
          tmpFormSection.setsectionFontType(getResultSetString(rs, "SECTION_FONT_TYPE"));
          tmpFormSection.setsectionFontStyle(getResultSetString(rs, "SECTION_FONT_STYLE"));
          tmpFormSection.setsectionPosition(getResultSetString(rs, "SECTION_POSITION"));
          tmpFormSection.setSubSection(getResultSetString(rs, "SUB_SECTION"));

          tmpFormSection.setCreatorName(UserInfoFactory.getUserFullName(tmpFormSection.getCreatorID()));
          tmpFormSection.setUpdaterName(UserInfoFactory.getUserFullName(tmpFormSection.getUpdaterID()));
          result.add(tmpFormSection);
        }

        FormElementDAObject formElementDAO = new FormElementDAObject(sessionContainer, dbConn);
        for (int i=0; i<result.size(); i++) {
          FormSection tmpFormSection = (FormSection) result.get(i);
          List formElementList = formElementDAO.getDetailListByFormSectionID(tmpFormSection.getID());
          tmpFormSection.setFormElementList(formElementList);
          result.set(i, tmpFormSection);
        }

        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  /**
   * get the permission for section and elements in the section.
   * @param formRecordID
   * @param formPageID
   * @param formSectionID
   * @param workflowStepID
   * @return
   * @throws ApplicationException
   */
  public synchronized boolean getFormSectionPermissionByRecordIDPageIDSectionIDStepID(Integer formRecordID, Integer formPageID, Integer formSectionID, Integer workflowStepID, boolean isViewUserStep) throws ApplicationException {
    String permission = "";
    boolean hasPermission = false;
    
    synchronized(dbConn) {
      try {
        MtmWfStepFormRecordDAObject mtmWfStepFormRecordDAO = new MtmWfStepFormRecordDAObject(sessionContainer, dbConn);
        FormElementDAObject formElementDAO = new FormElementDAObject(sessionContainer, dbConn);
        if (workflowStepID.intValue()>0){
          permission = mtmWfStepFormRecordDAO.getFormSectionPermissionByWorkflowStepIDFormRecordIDFormPageIDFormSectionID(workflowStepID, formRecordID, formPageID, formSectionID);
          if (!Utility.isEmpty(permission) && !"null".equals(permission)){
            if (!isViewUserStep){
              hasPermission = true; 
            }else if (isViewUserStep && !WorkflowStep.FORM_PROCESS_PERMISSION_READ.equals(permission)){
              hasPermission = true; 
            }             
          }else{
            hasPermission = formElementDAO.getSectionElementsPermissionByFormRecordIDPageIDSectionIDStepID(formRecordID,formPageID,formSectionID,workflowStepID,isViewUserStep);         
          }
        }else{
          hasPermission = true;    
        }
        
        return(hasPermission);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } 
    }
  }
 
  /**
   * get the permission for all sections and elements in the page.
   * @param formRecordID
   * @param formPageID
   * @param workflowStepID
   * @return
   * @throws ApplicationException
   */
  public synchronized boolean getPageSectionsPermissionByFormRecordIDPageIDStepID(Integer formRecordID, Integer formPageID, Integer workflowStepID, boolean isViewUserStep) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    Integer formSectionID = null;
    boolean hasPermission = false; 
    if ("null".equals(workflowStepID) || workflowStepID.intValue()<1){
      return true;   
    }    
    synchronized(dbConn) {
      try {      
        MtmWfStepFormRecordDAObject mtmWfStepFormRecordDAO = new MtmWfStepFormRecordDAObject(sessionContainer, dbConn);
        sqlStat.append("SELECT A.ID, A.TITLE, A.TITLE_ALT, A.DESCRIPTION, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE,  A.SECTION_FONT_COLOR,A.SECTION_FONT_SIZE, A.SECTION_FONT_TYPE,A.SECTION_FONT_STYLE,A.SECTION_POSITION,A.SUB_SECTION  ");
        sqlStat.append("FROM   FORM_SECTION A, MTM_FORM_PAGE_FORM_SECTION B, FORM_PAGE C ");
        sqlStat.append("WHERE  A.ID = B.SECTION_ID AND B.PAGE_ID = C.ID ");
        sqlStat.append("AND A.RECORD_STATUS = B.RECORD_STATUS AND B.RECORD_STATUS = C.RECORD_STATUS ");
        sqlStat.append("AND C.FORM_RECORD_ID = ? AND B.PAGE_ID = ? AND A.RECORD_STATUS = ? ");
        sqlStat.append("ORDER BY B.SECTION_SEQ ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, formRecordID);
        this.setPrepareStatement(preStat, 2, formPageID);
        this.setPrepareStatement(preStat, 3, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();        
        while (rs.next()) {  
          formSectionID = getResultSetInteger(rs, "ID");
          hasPermission = this.getFormSectionPermissionByRecordIDPageIDSectionIDStepID(formRecordID, formPageID, formSectionID, workflowStepID, isViewUserStep);
          if (hasPermission){
            break;  
          }            
        }
        return hasPermission;
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
}
